import Vue from 'vue'
import util from '@/libs/util'

// import Cookies from 'js-cookie'
let buttonArr = []
/** 权限指令**/
const has = Vue.directive('has', {
  bind: function (el, binding, vnode, data) {
    // 获取菜单路径
    var minleth = vnode.elm.baseURI.lastIndexOf('#')
    var maxleth = vnode.elm.baseURI.length
    var url = vnode.elm.baseURI.substring(minleth + 1, maxleth)
    if (!Vue.prototype.$_has(binding.value, url)) {
      el.style.display = 'none'
    }
  }
})
// 权限检查方法
Vue.prototype.$_has = function (value, url) {
  // if (util.cookies.get('menuButton') !== undefined) {
  //   buttonArr = JSON.parse(util.cookies.get('menuButton'))
  // }
  // console.log(util.db.get('buttonlist').__wrapped__.buttonlist)
  buttonArr = util.db.get('buttonlist').__wrapped__.buttonlist
  if (buttonArr !== undefined) {
    var pathButton = buttonArr.find(
      r =>
        r.path === url && r.code === value
    )
    if (pathButton === undefined || pathButton === null) {
      return false
    } else {
      return true
    }
  } else {
    return false
  }
}
export default has
